using System.Collections.Generic;
using Taobao.Tools2.Domain.EntityFramework.Mappings;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data;
using System; 
using System.Web;

namespace Taobao.Tools2.Domain.EntityFramework
{
    public class EfDbContextBase : System.Data.Entity.DbContext,IDisposable
    { 
        #region Const
        #endregion

        #region Ctor

        public EfDbContextBase()
            : base()
        {
        }


        #endregion

        #region Properties

        public ICollection<IMapping> Mappings { get; set; }

        #endregion

        #region Override

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            if (Mappings == null || Mappings.Count == 0)
                return;
            foreach (var mapping in Mappings)
            {
                mapping.RegistTo(modelBuilder.Configurations);
            }
        }

        public void Detach(object entity)
        {
            ((IObjectContextAdapter)this).ObjectContext.Detach(entity);
        }

        public override int SaveChanges()
        { 
            return base.SaveChanges();
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }

    public class EfDbContext : EfDbContextBase, IDisposable
    {
        public EfDbContext()
            : base()
        {
            Mappings = new List<IMapping>() { 
                new DistributorMapping(),
                new LogMapping(),
                new OrderInfoMapping(),
                new OutSentMapping(),
                //new OutSentNumMapping(),
                new RefundMapping(),
                new SenderMapping(),
                new ShopMapping(),
                new TradeMapping(),
                new PromotionDetailMapping(),
                new ItemMapping()
            };
        }
    }
}